﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:TagCloud runat=server></{0}:TagCloud>")> _
    Public Class TagCloud
        Inherits WebControl
        Private _siteId As Integer = -1
        Private _instanceId As Integer = -1
        Private _cssOne As String = "tagcssone"
        Private _cssTwo As String = "tagcsstwo"
        Private _cssThree As String = "tagcssthree"
        Public Property TabId() As Integer = -1
        Public Property PortalId() As Integer
            Get
                Return _siteId
            End Get
            Set(ByVal value As Integer)
                _siteId = value
            End Set
        End Property
        Public Property ModuleId() As Integer
            Get
                Return _instanceId
            End Get
            Set(ByVal value As Integer)
                _instanceId = value
            End Set
        End Property
        Public Property CSSOne() As String
            Get
                Return _cssOne
            End Get
            Set(ByVal value As String)
                _cssOne = value
            End Set
        End Property
        Public Property CSSTwo() As String
            Get
                Return _cssTwo
            End Get
            Set(ByVal value As String)
                _cssTwo = value
            End Set
        End Property
        Public Property CSSThree() As String
            Get
                Return _cssThree
            End Get
            Set(ByVal value As String)
                _cssThree = value
            End Set
        End Property
        Public Property ForumIds() As String = String.Empty
        Public Property TagCount() As Integer = 15
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            Dim forumUser As User = Nothing
            If String.IsNullOrEmpty(ForumIds) Then
                Dim uc As New UserController
                forumUser = uc.GetUser(PortalId, ModuleId)
                If String.IsNullOrEmpty(forumUser.UserForums) Then
                    Dim fc As New ForumController
                    ForumIds = fc.GetForumsForUser(forumUser.UserRoles, PortalId, ModuleId)
                Else
                    ForumIds = forumUser.UserForums
                End If
            End If
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim db As New Data.Common
            Dim dr As IDataReader = db.TagCloud_Get(PortalId, ModuleId, ForumIds, TagCount)
            Dim ctlUtils As New ControlUtils
            Dim sURL As String = String.Empty
            While dr.Read()
                Dim priority As Integer = 1
                Dim tagName As String = String.Empty
                Dim css As String = String.Empty
                priority = Integer.Parse(dr("Priority").ToString)
                tagName = dr("TagName").ToString
                Select Case priority
                    Case 1
                        css = CSSOne
                    Case 2
                        css = CSSTwo
                    Case 3
                        css = CSSThree
                End Select
                writer.Write("<span class=""" & css & """>")
                writer.Write("<a href=""")
                sURL = ctlUtils.BuildUrl(TabId, ModuleId, String.Empty, String.Empty, -1, -1, Integer.Parse(dr("TagID").ToString), -1, Utilities.CleanName(tagName), 1, -1)
                writer.Write(sURL)
                writer.Write(""" title=""" & HttpUtility.HtmlAttributeEncode(tagName) & """>" & tagName & "</a></span> ")
            End While
            dr.Close()
            dr.Dispose()
        End Sub

        Private Sub TagCloud_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            EnableViewState = False
        End Sub
    End Class
End Namespace